﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace XPressify.Controllers
{
    public class TestController : BaseController
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(string content)
        {
            return View();
        }

        public ActionResult RichTextEditor()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult RichTextEditor(string content)
        {
            return View();
        }

        public ActionResult UploadSimple()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadSimple(string fileUpload)
        {
            var user = CurrentUser; //
            var path = Server.MapPath("~/Media/" + user.Id + "/"); //
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path); // create directory
            }
            foreach (var postedFile in from string file in Request.Files select Request.Files[file])
            {
                postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
                //                postedFile.SaveAs(Server.MapPath("~/Images/") + Path.GetFileName(postedFile.FileName));
            }

            return View();
        }


        public ActionResult UploadAdvanced()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UploadAdvanced(string token, HttpPostedFileBase fileData)
        {
            var ticket = FormsAuthentication.Decrypt(token);
            if (ticket != null)
            {
                var identity = new FormsIdentity(ticket);
                if (identity.IsAuthenticated)
                {
                    try
                    {
                        var user = CurrentUser; //
                        var path = Server.MapPath("~/Media/" + user.Id + "/"); //
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path); // create directory
                        }
                        fileData.SaveAs(path + Path.GetFileName(fileData.FileName));


                        //Save file and other code removed
                        return Content("File uploaded successfully!");
                    }
                    catch (Exception ex)
                    {
                        return Content("Error uploading file: " + ex.Message);
                    }
                }
            }
            throw new InvalidOperationException("The user is not authenticated.");
        }

    }
}
